package com.haloer.net
{
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;

	public class HTTP
	{
		private var _loader :URLLoader;
		private var _method :String;
		
		public var onComplete :Function;
		public var onError :Function;
		public var url :String;
		public var data :Object;
		
		public function HTTP(dataFormat :String = "text", method :String = "get")
		{
			_loader = new URLLoader();
			_loader.dataFormat = dataFormat;
			_method = method;
		}
		
		public function load( path :String = "" ) :void {
			if( path == "" && url == "" ) {
				throw new Error("无效的url！");
			}
			
			if( path ) {
				url = path;
			}
			
			addEvent();
			
			var urlRequest :URLRequest = new URLRequest( url );
			urlRequest.method = _method;
			
			if( data ) {
				var urlVariables :URLVariables = new URLVariables();
				for( var key :String in data ) {
					urlVariables[ key ] = data[ key ];
				}
				urlRequest.data = urlVariables;
			}
			
			_loader.load( urlRequest );
		}
		
		private function addEvent() :void {
			_loader.addEventListener(Event.COMPLETE, complete);
			_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatus);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_loader.addEventListener(Event.OPEN, openHandler);
			_loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		
		private function removeEvent() : void
		{
			_loader.removeEventListener(Event.COMPLETE, complete);
			_loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatus);
			_loader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_loader.removeEventListener(Event.OPEN, openHandler);
			_loader.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
		}// end function
		
		private function progressHandler(event:ProgressEvent) : void
		{
			return;
		}// end function
		
		private function openHandler(event:Event) : void
		{
			return;
		}// end function
		
		private function complete(event:Event) : void
		{
			removeEvent();
			if (onComplete is Function)
			{
				onComplete(event.target.data);
			}
			return;
		}// end function
		
		private function httpStatus(event:HTTPStatusEvent) : void
		{
			return;
		}// end function
		
		private function ioErrorHandler(event:IOErrorEvent) : void
		{
			removeEvent();
			if (onError is Function)
			{
				onError();
			}
			return;
		}// end function
		
		private function securityErrorHandler(event:SecurityErrorEvent) : void
		{
			removeEvent();
			if (onError is Function)
			{
				onError();
			}
			return;
		}// end function
		
		public function get loader() : URLLoader
		{
			return _loader;
		}
	}
}